﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Text.RegularExpressions;
using Bitly;

namespace Twittit.Business
{
    // REMARQUE : vous pouvez utiliser la commande Renommer du menu Refactoriser pour changer le nom de classe "Twittit" à la fois dans le code et le fichier de configuration.
    public class TwittService
    {
        
        private Twittit.DataAccess.DatabaseEntities data;
        private static TwittService instance;

        private TwittService()
        {
            data = new Twittit.DataAccess.DatabaseEntities();
        }

        public static TwittService getInstance()
        {
            if (instance == null)
                instance = new TwittService();
            return instance;
        }

        public Twittit.DataAccess.Twit addTwitt(int userId, String content)
        {
            Twittit.DataAccess.Twit t = new Twittit.DataAccess.Twit();
            String response = this.ShortenURL(content);
            t.text = response;
            t.user_id = userId;
            t.date = DateTime.Now;
            t.active = 1;
            this.data.Twit.AddObject(t);
            this.data.SaveChanges();
            
            MatchCollection m = Regex.Matches(content, @"\#\w+\b");
            foreach (Match i in m)
            {
                TwittService.getInstance().addTag(t.id, i.ToString().Remove(0, 1));
            }

            return t;


        }

        public int removeTwitt(int twittId, int userId)
        {
            Twittit.DataAccess.Twit twitt = this.data.Twit.Where( t=> t.id == twittId).First();
            if (twitt.user_id == userId)
            {
                try
                {
                    try
                    {
                        List<Twittit.DataAccess.TwittitTag> tags = data.TwittitTag.Where(t => t.twittit_id == twittId).ToList();
                        foreach (DataAccess.TwittitTag t in tags)
                            TwittitTagService.getInstance().remove(t);
                    }
                    catch (Exception) { }

                    data.Twit.DeleteObject(twitt);
                    data.SaveChanges();
                    return 0;
                }
                catch (Exception e)
                {
                    return 1;
                }
                
            }
            return 1;
            
        }

        public Twittit.DataAccess.Tag addTag(int twittit_id, String tag)
        {
            Twittit.DataAccess.Tag t = Twittit.Business.TagService.getInstance().getTag(tag);
            Twittit.DataAccess.TwittitTag tt = Twittit.Business.TwittitTagService.getInstance().getTwittitTag(twittit_id, t);

            return t;
        }

        private String ShortenURL(string msgLong)
        {
            String response = "";
            String query = "";
            String regex = @"((www\.|(http|https|ftp|news|file)+\:\/\/)[&#95;.a-z0-9-]+\.[a-z0-9\/&#95;:@=.+?,##%&~-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])";
            MatchCollection listMatches = Regex.Matches(msgLong, regex);
            foreach (Match msg in listMatches)
            {
                query = msg.ToString();
                if (!msg.Equals(""))
                {
                    response = API.Bit("bragollach", "R_ca2161531e143be4f1e75e04e6f4f839", query, "Shorten");
                    if (response.Trim() != "")
                        msgLong = msgLong.Replace(query, response);
                }
            }
            return msgLong;
        }
    }
}
